package com.example.sherlockzhong.plush_v2.Models;

import com.example.sherlockzhong.plush_v2.Constant;
import com.example.sherlockzhong.plush_v2.Constants.UrlConstant;
import com.example.sherlockzhong.plush_v2.Utils.NetworkUtils;
import com.example.sherlockzhong.plush_v2.Utils.StorageUtils;

import org.json.JSONArray;
import org.json.JSONTokener;


/**
 * Created by DosonLeung on 2014/9/19.
 */

public class PushModel {

    public static JSONArray listAllPosters(String type) {
        String param = "";
        if (Constant.STUDENT_ID != null) {
            if (!type.equalsIgnoreCase("0")) {
                param = "&type=" + type;
            }
            try {
                String result = NetworkUtils.httpGet(String.format(UrlConstant.allPostersUrl, Constant.STUDENT_ID) + param);
                JSONTokener jsonTokener = new JSONTokener(result);
                JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
                StorageUtils.saveAsFile("AllPosters.json", jsonArray.toString());
                return jsonArray;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONArray listSubscribedPosters() {
        if (Constant.STUDENT_ID != null && Constant.STUDENT_TOKEN != null) {
            try {
                String result = NetworkUtils.httpGet(String.format(UrlConstant.subscribedPostersUrl, Constant.STUDENT_ID, Constant.STUDENT_TOKEN));
                JSONTokener jsonTokener = new JSONTokener(result);
                JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
                StorageUtils.saveAsFile("SubscribedPosters.json", jsonArray.toString());
                return jsonArray;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONArray retrievePosters(String type) {
        try {
            String fileName;
            if (type.equalsIgnoreCase("all")) {
                fileName = "AllPosters.json";
            } else {
                fileName = "SubscribedPosters.json";
            }
            String raw = StorageUtils.retrieveFromFile(fileName);
            JSONTokener jsonTokener = new JSONTokener(raw);
            JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONArray getSubscribedNews() {
        if (Constant.STUDENT_ID != null && Constant.STUDENT_TOKEN != null) {
            try {
                String result = NetworkUtils.httpGet(String.format(UrlConstant.subscribedNewsUrl, Constant.STUDENT_ID, Constant.STUDENT_TOKEN));
                JSONTokener jsonTokener = new JSONTokener(result);
                JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
                return jsonArray;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONArray getPosterNews(String id) {
        try {
            String result = NetworkUtils.httpGet(String.format(UrlConstant.posterNewsUrl, id));
            JSONTokener jsonTokener = new JSONTokener(result);
            JSONArray jsonArray = (JSONArray) jsonTokener.nextValue();
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public synchronized static boolean subscribe(String posterID, boolean isUnsubscribe) {
        if (Constant.STUDENT_ID != null && Constant.STUDENT_TOKEN != null) {
            try {
                String url = (isUnsubscribe) ? UrlConstant.unsubscribeUrl : UrlConstant.subscribeUrl;
                String result = NetworkUtils.httpGet(String.format(url, Constant.STUDENT_ID, Constant.STUDENT_TOKEN, posterID));
                if (result != null) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /*
    public synchronized boolean updateSubscribedFile(ArrayList<String> deleteQueue){
        String fileName = "SubscribedPosters.json";
        if(deleteQueue!=null){
            JSONArray posterArray = retrievePosters("Subscribe");
            int length = posterArray.length();
            JSONObject posterObject;
            String id;
            try {
                for (int i = 0; i < length; i++) {
                    posterObject = posterArray.getJSONObject(i);
                    id = posterObject.getString("id");
                    if(deleteQueue.contains(id)){
                        posterArray.remove(i);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    } */
}
